package com.heye.imheye.controller;

import com.heye.imheye.dto.RegisterReq;
import com.heye.imheye.entity.User;
import com.heye.imheye.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/register")
@RequiredArgsConstructor
public class RegisterController {

    private final PasswordEncoder passwordEncoder;
    private final UserRepository userRepository;

    @GetMapping
    public String index() {
        return "register";
    }

    @ResponseBody
    @PostMapping
    public void register(@RequestBody RegisterReq req) {
        User user = new User();
        user.setUsername(req.username());
        user.setPassword(passwordEncoder.encode(req.password()));
        userRepository.save(user);
    }
}
